"""
check if login or not
"""
import portalclient

nt=portalclient.PortalClient()


@auth.requires_login()
def management():
    """get server list from API"""
    sysInstance =nt.servers.list()
    """synchronize with DB"""
    if sysInstance:
        for item in sysInstance:
            rows = db(db.instances.id==item.id).select() 
            if rows:
                for row in rows:
                    if row.instance_name !=item.name:
                        db(db.instances.id==item.id).update(instance_name=item.name,instance_flavor_id=item.flavor['id'],
                                                        instance_image_id=item.image['id'],
                                                        instance_private_ip=item.addresses['private'][0]['addr'],
                                                        #instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=item.created,
                                                        instance_key_name=item.key_name,status=item.status)
                    else:
                        if row.instance_private_ip != item.addresses['private'][0]['addr']:
                            db(db.instances.id==item.id).update(instance_private_ip=item.addresses['private'][0]['addr'],
                                                        #instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=item.created,
                                                        instance_key_name=item.key_name,status=item.status)
                        else:
                       # if row.instance_public_ip !=item.addresses['public'][0]['addr']:
                        #    db(db.instances.id==item.id).update( instance_public_ip=item.addresses['public'][0]['addr'],
                         #                               instance_created_time=item.created,
                          #                              instance_key_name=item.key_name,status=item.status)
                        #else:
                            if row.status !=item.status:
                                db(db.instances.id==item.id).update( status=item.status)
                
            else:
                db.instances.insert(id=item.id,instance_name=item.name,instance_flavor_id=item.flavor['id'],
                                                        instance_image_id=item.image['id'],
                                                        instance_private_ip=item.addresses['private'][0]['addr'],
                                                       # instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=item.created,
                                                        instance_key_name=item.key_name,status=item.status,is_full='F') 
    """get list from DB"""
    sysInstance = db(db.instances).select(orderby=db.instances.id)
    return locals()

    #return dict()
def create():
    flavorList = db(db.flavors.is_delete !='T').select(orderby=db.flavors.id)
    imageList = db(db.images.is_delete !='T').select(orderby=db.images.id)
    if request.vars:
        try:
            result=nt.servers.create(request.vars.name,request.vars.image,request.vars.flavor)
            if result:
                """wait util have address"""
            while result.addresses == None:
                pass
            db.instances.insert(id=result.id,instance_name=result.name,instance_flavor_id=result.flavor['id'],
                                                        instance_image_id=result.image['id'],
                                                        instance_private_ip=result.addresses['private'][0]['addr'],
                                                       # instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=result.created,
                                                        instance_key_name=result.key_name,status=result.status,is_full='F')
            redirect(URL('management'))  
        except:
            print('boot new instance fail')
            redirect(URL('management'))  
           
    return locals()

def delete():
    id=request.args[0]
    try:
        db(db.instances.id==id).delete()
        nt.servers.delete(id)
        redirect(URL('management'))
    except:
        redirect(URL('management'))
    return locals()